package cn.com.dycc.party_org.organize.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.dycc.common.log.Constants;
import cn.com.dycc.common.log.SystemServiceLog;
import cn.com.dycc.party_org.member.dao.MemberDao;
import cn.com.dycc.party_org.organize.dao.OrganizeDao;
import cn.com.dycc.party_org.organize.entity.BasicUserMation;
import cn.com.dycc.party_org.organize.entity.OrganizeEntity;
import cn.com.dycc.party_org.organize.entity.OrganizeUserInfo;
import cn.com.dycc.party_org.organize.entity.PartyInfo;
import cn.com.dycc.party_org.organize.param.CountPartyParam;
import cn.com.dycc.party_org.organize.param.OrganizeParam;
import cn.com.dycc.party_org.organize.param.PartyInfoParam;
import cn.com.dycc.sys_manage.deptsysmanagement.dao.DeptDao;
import cn.com.dycc.sys_manage.deptsysmanagement.entity.Dept;
import cn.com.dycc.topic_activity.dao.Pb_Activity_DeptDao;
import cn.com.dycc.util.DateUtil;
import cn.com.dycc.util.IntegerUtil;
import cn.com.dycc.util.OrganiTreeUtil;
import cn.com.dycc.util.PartyCheckTreeUtil;
import cn.com.dycc.util.PartyTreeLineUtil;
import cn.com.dycc.util.PartyTreeUtil;

@Service("organizeService")
@Transactional(rollbackFor=Exception.class)
public class OrganizeServiceImpl implements OrganizeService{
	
	@Resource(name="organizeDao")
	private OrganizeDao organizeDao;
	
	@Resource(name="deptDao")
	private DeptDao deptDao;
	
	@Resource(name="memberDao")
	private MemberDao memberDao;
	
	@Resource(name="pb_Activity_DeptDao")
	private Pb_Activity_DeptDao pb_Activity_DeptDao;
	
	
	//增加党组织 HttpServletRequest request,
	@SystemServiceLog(description=Constants.ORGANIZE_ADD)
	public String addDeptParty(HttpServletRequest request,OrganizeParam organizeParam) throws NotFoundOrganizeException {
		
		String org_id = request.getSession().getAttribute("org_id").toString();
//		String org_id = "1";
		//党组织归属单位不可为空
		if(organizeParam.getOwn_dept_id()==null||organizeParam.getOwn_dept_id().trim().isEmpty()){
			throw new NotFoundOrganizeException("请选择党组织归属单位！");
		}
		Dept dept = deptDao.findDeptByDeptNo(organizeParam.getOwn_dept_id(),org_id);
		if(dept==null){
			throw new NotFoundOrganizeException("党组织归属单位未存在！");
		}
		//党组织归属为整型
		if(!(IntegerUtil.isNumeric(organizeParam.getOwn_dept_id()))){
			throw new NotFoundOrganizeException("党组织归属单位属性为整型！");
		}
		//党组织上级id不可为空
		if(organizeParam.getPid()==null||organizeParam.getPid().trim().isEmpty()){
			throw new NotFoundOrganizeException("请选择党组织归属上级！");
		}
		//党组织上级id为整型
//		System.out.println("s="+organizeParam.getPid());
		if(!(IntegerUtil.isNumeric(organizeParam.getPid()))){
			throw new NotFoundOrganizeException("党组织归属上级为整型！");
		}
		
		//党组织命名不可为空
		if(organizeParam.getDept_name()==null||organizeParam.getDept_name().trim().isEmpty()){
			throw new NotFoundOrganizeException("请填写党组织名称！");
		}
		//党组织命名同部门的同级不同名
		CountPartyParam countPartyParam = new CountPartyParam();
		countPartyParam.setDept_name(organizeParam.getDept_name());
		countPartyParam.setOrg_id(org_id);
		countPartyParam.setOwn_dept_id(organizeParam.getOwn_dept_id());
		countPartyParam.setPid(organizeParam.getPid());
		int count = organizeDao.countParty(countPartyParam);
		if(count>=1){
			throw new NotFoundOrganizeException("该归属部门下的同级已存在相同命名的党组织！");
		}
		//order_no只能为整型
		if(organizeParam.getOrder_no()!=null&&!(IntegerUtil.isNumeric(organizeParam.getOrder_no().toString()))){
			throw new NotFoundOrganizeException("党组织的排序只能为整型！");
		}
		//联系方式假如不为空，需要验证是否为正确的
		boolean b = false;
		if(organizeParam.getDept_tel()!=null){
			if(organizeParam.getDept_tel().length()==11){
				b = IntegerUtil.isMobile(organizeParam.getDept_tel());
			}else{
				b = IntegerUtil.isPhone(organizeParam.getDept_tel());
			}
			if(!b){
				throw new NotFoundOrganizeException("请输入正确的联系方式！");
			}
		}
		//创建人
		String opt_user_id = request.getSession().getAttribute("id").toString();
		if(opt_user_id==null){
			throw new NotFoundOrganizeException("请登录！");
		}
//		String opt_user_id = "1";
		organizeParam.setOrg_id(org_id);
		organizeParam.setOpt_user_id(opt_user_id);
		
		int add = organizeDao.addDeptParty(organizeParam);
		if(add==0){
			throw new NotFoundOrganizeException("增加党组织失败！");
		}
		return "增加党组织成功！";
	}
	
	//查询所有显示党组织树 用于点击后弹出的党组织树 HttpServletRequest request 和人员的通讯录,
	public OrganizeUserInfo findOrganizeUser(HttpServletRequest request,PartyInfoParam partyInfoParam)
			throws NotFoundOrganizeException {
		
		String org_id = request.getSession().getAttribute("org_id").toString();
//		String org_id = "1";
		
		if(partyInfoParam.getPid()==null||partyInfoParam.getPid().trim().isEmpty()){
			throw new NotFoundOrganizeException("请选择正确的查询id！");
		}
		
		partyInfoParam.setOrg_id(org_id);
		OrganizeUserInfo oui = new OrganizeUserInfo();
		
		
		try {
			
			List<PartyInfo> listPartyInfo = organizeDao.findListPartyInfo(partyInfoParam);
			//党组织最顶级只需要查询党组织信息，否则查询上级部门的人员和该部门下的下一级子部门
			if("0".equals(partyInfoParam.getPid())){
				oui.setListPartyInfo(listPartyInfo);
			}else{
				List<BasicUserMation> listBasicUserMation = memberDao.findMemBerByOrganize(org_id,partyInfoParam.getPid());
				oui.setListPartyInfo(listPartyInfo);
				oui.setListBasicUserMation(listBasicUserMation);
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new NotFoundOrganizeException("查询失败，请稍后重试！");
		}
		
		return oui;
	}
	
	//查询所有显示党组织树 HttpServletRequest request, 
	public List<Object> findListPartyInfo(HttpServletRequest request,PartyInfoParam partyInfoParam)
			throws NotFoundOrganizeException {
		
		String org_id = request.getSession().getAttribute("org_id").toString();
//		String org_id = "1";
		
		partyInfoParam.setOrg_id(org_id);
		
		List<PartyInfo> list = new ArrayList<PartyInfo>();
		
		List<Object> lisTree = new ArrayList<Object>();
		
		try {
			list = organizeDao.findListPartyInfo(partyInfoParam);
			
			PartyTreeUtil p = new PartyTreeUtil();
			
			lisTree = p.PartyList(list);
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new NotFoundOrganizeException("查询失败，请稍后重试！");
		}
		
		
		return lisTree;
	}
	
	//显示党组织树，用于用户的党组织显示
	public List<Object> findListOrganiTree(HttpServletRequest request, PartyInfoParam partyInfoParam)
			throws NotFoundOrganizeException {
		
		String org_id = request.getSession().getAttribute("org_id").toString();
//		String org_id = "1";
		
		partyInfoParam.setOrg_id(org_id);
		
		List<PartyInfo> list = new ArrayList<PartyInfo>();
		
		List<Object> lisTree = new ArrayList<Object>();
		
		try {
			list = organizeDao.findListPartyInfo(partyInfoParam);
			
			OrganiTreeUtil o = new OrganiTreeUtil();
			
			lisTree = o.PartyList(list);
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new NotFoundOrganizeException("查询失败，请稍后重试！");
		}
		
		return lisTree;
	}
	
	//根据id查询党组织信息
	public OrganizeEntity findOrganizeByDeptId(HttpServletRequest request,String dept_id) throws NotFoundOrganizeException {
		
		if(dept_id==null||dept_id.trim().isEmpty()){
			throw new NotFoundOrganizeException("请选择需要查询的党组织！");
		}
		if(!(IntegerUtil.isNumeric(dept_id))){
			throw new NotFoundOrganizeException("查询的党组织id需要为整型！");
		}
		
		String org_id = request.getSession().getAttribute("org_id").toString();
		
		OrganizeEntity organizeEntity = new OrganizeEntity();
		
		try {
			
			organizeEntity = organizeDao.findOrganizeByDeptId(dept_id,org_id);
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new NotFoundOrganizeException("查询失败！");
		}
		return organizeEntity;
	}
	
	//根据id更新党组织信息
	@SystemServiceLog(description=Constants.ORGANIZE_UPDATE)
	public String updateOrganize(HttpServletRequest request,OrganizeParam organizeParam) throws NotFoundOrganizeException {
		
		String org_id = request.getSession().getAttribute("org_id").toString();
//		String org_id = "1";
		
		if(organizeParam.getDept_id()==null||organizeParam.getDept_id().trim().isEmpty()){
			throw new NotFoundOrganizeException("请选择需要更新的党组织！");
		}
		//党组织归属单位不可为空
		if(organizeParam.getOwn_dept_id()==null||organizeParam.getOwn_dept_id().trim().isEmpty()){
			throw new NotFoundOrganizeException("请选择党组织归属单位！");
		}
		Dept dept = deptDao.findDeptByDeptNo(organizeParam.getOwn_dept_id(),org_id);
		if(dept==null){
			throw new NotFoundOrganizeException("党组织归属单位未存在！");
		}
		//党组织归属为整型
		if(!(IntegerUtil.isNumeric(organizeParam.getOwn_dept_id()))){
			throw new NotFoundOrganizeException("党组织归属单位属性为整型！");
		}
		//党组织上级id不可为空
		if(organizeParam.getPid()==null||organizeParam.getPid().trim().isEmpty()){
			throw new NotFoundOrganizeException("请选择党组织归属上级！");
		}
		//党组织上级id为整型
//		System.out.println("s="+organizeParam.getPid());
		if(!(IntegerUtil.isNumeric(organizeParam.getPid()))){
			throw new NotFoundOrganizeException("党组织归属上级为整型！");
		}
		
		//党组织命名不可为空
		if(organizeParam.getDept_name()==null||organizeParam.getDept_name().trim().isEmpty()){
			throw new NotFoundOrganizeException("请填写党组织名称！");
		}
		
		//判断名称是否已经改变
		OrganizeEntity oe = organizeDao.findOrganizeByDeptId(organizeParam.getDept_id(),org_id);
		//||!(oe.getPid().equals(organizeParam.getPid())) 移动也要进行判断是否同名同级
		if(!(oe.getDept_name().equals(organizeParam.getDept_name()))){
			//党组织命名同部门的同级不同名
			CountPartyParam countPartyParam = new CountPartyParam();
			countPartyParam.setDept_name(organizeParam.getDept_name());
			countPartyParam.setOrg_id(org_id);
			countPartyParam.setOwn_dept_id(organizeParam.getOwn_dept_id());
			countPartyParam.setPid(organizeParam.getPid());
			int count = organizeDao.countParty(countPartyParam);
			if(count>=1){
				throw new NotFoundOrganizeException("该归属部门下的同级已存在相同命名的党组织！");
			}
		}
		
		//禁用党组织时，是否在该党组织下有党员
		if("N".equals(organizeParam.getState())){
			int countMember = memberDao.findCountByDeptId(org_id, organizeParam.getDept_id());
			if(countMember>=1){
				throw new NotFoundOrganizeException("该党组织下还存在人员！");
			}
		}
		
		//order_no只能为整型
		if(organizeParam.getOrder_no()!=null&&!(IntegerUtil.isNumeric(organizeParam.getOrder_no().toString()))){
			throw new NotFoundOrganizeException("党组织的排序只能为整型！");
		}
		
		//联系方式假如不为空，需要验证是否为正确的
		boolean b = false;
		if(organizeParam.getDept_tel()!=null){
			if(organizeParam.getDept_tel().length()==11){
				b = IntegerUtil.isMobile(organizeParam.getDept_tel());
			}else{
				b = IntegerUtil.isPhone(organizeParam.getDept_tel());
			}
			if(!b){
				throw new NotFoundOrganizeException("请输入正确的联系方式！");
			}
		}
		
		//创建人
		String update_user_id = request.getSession().getAttribute("id").toString();
		if(update_user_id==null){
			throw new NotFoundOrganizeException("请登录！");
		}
		
		String format = "yyyy-MM-dd HH:mm:ss";
		
		String update_date = DateUtil.getNowDate(format);
//		String opt_user_id = "1";
//		organizeParam.setOrg_id(org_id);
		organizeParam.setOpt_user_id(update_user_id);
		organizeParam.setUpdate_date(update_date);
		organizeParam.setOrg_id(org_id);
		
		int up = organizeDao.updateOrganize(organizeParam);
		if(up==0){
			throw new NotFoundOrganizeException("更新失败！");
		}
		
		return "更新成功！";
	}
	
	//根据党组织id删除党组织信息
	@SystemServiceLog(description=Constants.ORGANIZE_DELETE)
	public String deleteOrganize(HttpServletRequest request,String dept_id) throws NotFoundOrganizeException {
		
		if(dept_id==null||dept_id.trim().isEmpty()){
			throw new NotFoundOrganizeException("请选择需要删除的党组织！");
		}
		
		if(!(IntegerUtil.isNumeric(dept_id))){
			throw new NotFoundOrganizeException("删除的党组织信息id为整型！");
		}
		
		String org_id = request.getSession().getAttribute("org_id").toString();
		
		int countMember = memberDao.findCountByDeptId(org_id,dept_id);
		if(countMember>=1){
			throw new NotFoundOrganizeException("该党组织下还存在人员！");
		}
		
		//查询这个部门下是否存在子部门
		int countPidDept = organizeDao.findCountByPid(org_id,dept_id);
		if(countPidDept>=1){
			throw new NotFoundOrganizeException("该党组织下还存在子部门！");
		}
		
		int countDel = organizeDao.deleteOrganize(dept_id,org_id);
		if(countDel==0){
			throw new NotFoundOrganizeException("删除失败！");
		}
		
		return "删除成功！";
	}
	
	//显示党组织列表
	public List<Object> findListOrganiLine(HttpServletRequest request, PartyInfoParam partyInfoParam)
			throws NotFoundOrganizeException {
		
		String org_id = request.getSession().getAttribute("org_id").toString();
//		String org_id = "1";
		
		partyInfoParam.setOrg_id(org_id);
		
		List<PartyInfo> list = new ArrayList<PartyInfo>();
		
		List<Object> lisTree = new ArrayList<Object>();
		
		try {
			list = organizeDao.findListPartyInfo(partyInfoParam);
			
			OrganiTreeUtil o = new OrganiTreeUtil();
			
			lisTree = o.PartyLine(list);
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new NotFoundOrganizeException("查询失败，请稍后重试！");
		}
		
		return lisTree;
	}
	
	//显示默认选择的党组织树
	public List<Object> findListCheckTree(HttpServletRequest request, PartyInfoParam partyInfoParam,String[] listDeptId)
			throws NotFoundOrganizeException {
		
			
		String org_id = request.getSession().getAttribute("org_id").toString();
//		String org_id = "1";
		
		partyInfoParam.setOrg_id(org_id);
		
		List<PartyInfo> list = new ArrayList<PartyInfo>();
		
		List<Object> lisTree = new ArrayList<Object>();
		
		try {
			list = organizeDao.findListPartyInfo(partyInfoParam);
			
			PartyCheckTreeUtil p = new PartyCheckTreeUtil();
			
			lisTree = p.PartyList(list,listDeptId);
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new NotFoundOrganizeException("查询失败，请稍后重试！");
		}
		
		return lisTree;
	}
	
	//显示党组织树，父可以查子，子不能查父
	public List<Object> findTreeByPid(HttpServletRequest request, PartyInfoParam partyInfoParam,String[] listDeptId)
			throws NotFoundOrganizeException {
		
		String org_id = request.getSession().getAttribute("org_id").toString();
//		String org_id = "1";
		//获取到当前的登录人id为pid查询
		String party_dept = request.getSession().getAttribute("dept_id").toString();
//		System.out.println("party_dept="+party_dept);
		
		partyInfoParam.setOrg_id(org_id);
		
		List<PartyInfo> list = new ArrayList<PartyInfo>();
		
		List<Object> lisTree = new ArrayList<Object>();
		
		try {
			list = organizeDao.findListPartyInfo(partyInfoParam);
//			System.out.println(list);
			
			PartyCheckTreeUtil p = new PartyCheckTreeUtil();
			
			lisTree = p.PartyCheckListByPid(list, party_dept, listDeptId);
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new NotFoundOrganizeException("查询失败，请稍后重试！");
		}
		
		
		return lisTree;
	}
	
	//显示党组织树，父可以查子，子不能查父   列表 HttpServletRequest request,
	public List<String> findTreeByPidLine( HttpServletRequest request,PartyInfoParam partyInfoParam,String party_dept)
			throws NotFoundOrganizeException {
		String org_id = request.getSession().getAttribute("org_id").toString();
//		String org_id = "1";
		//获取到当前的登录人id为pid查询
//		String party_dept = request.getSession().getAttribute("dept_id").toString();
//		System.out.println("party_dept="+party_dept);
		
		partyInfoParam.setOrg_id(org_id);
		
		List<PartyInfo> list = new ArrayList<PartyInfo>();
		
		List<String> lisTree = new ArrayList<String>();
		
		try {
			list = organizeDao.findListPartyInfo(partyInfoParam);
//			System.out.println(list);
			
			PartyTreeLineUtil p = new PartyTreeLineUtil();
			
			lisTree = p.PartyListByPidLine(list,party_dept);
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new NotFoundOrganizeException("查询失败，请稍后重试！");
		}
		
		return lisTree;
	}
		
	

}
